/**
 * @description json schema 验证中间件
 * @author 刘煎蛋
 */

const { ErrorModel } = require('../model/ResModel')
const { jsonSchemaFileInfo } = require('../model/ErrorInfo')

function genValidator( validateFn ) {
    // 定义中间件函数
    async function validator( ctx, next ) {
        const data = ctx.request.body;
        const error = validateFn( data );
        if ( error ) {
            ctx.body = new ErrorModel(jsonSchemaFileInfo);
            return;
        }
        // 验证成功
        await next();
    }
    return validator;
}

module.exports = {
    genValidator
}